package com.epfresh.supplier.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.global.BaseFragment;
import com.epfresh.api.utils.T;
import com.epfresh.api.widget.keyboard.KeyboardUtil;
import com.epfresh.api.widget.tablayout.CommonTabLayout;
import com.epfresh.api.widget.tablayout.TabEntity;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.fragment.SoldFragment;

import java.util.ArrayList;

public class SpotGoodsSearchActivity extends BaseActivity implements OnTabSelectListener {
    private ImageView ivBack;
    private EditText etContent;
    private TextView tvSearch;
    private CommonTabLayout commonTabLayout;
    private ViewPager vp;

    private SoldFragment soldUpFragment;
    private SoldFragment soldDraftFragment;
    private SoldFragment soldDownFragment;
    private BaseFragment currentFragment;

    private String[] tabs = {"已上架(0)", "草稿(0)", "已下架(0)"};
    private int pageCount = 3;
    private boolean isDc = false;//显示库存与否
    private int productType = 1;// 1现货点进来的,2预售点进来的
    public int upCount, draftCount, downCount;
    private String keyword;
    private boolean isInit = false;

    public String getKeyword() {
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spot_goods_search);
        initView();
        initClick();
    }

    private void initView() {
        ivBack = findViewById(R.id.iv_back);
        etContent = findViewById(R.id.et_content);
        tvSearch = findViewById(R.id.tv_search);

        commonTabLayout = findViewById(R.id.ly_navigation);
        vp = findViewById(R.id.vp);

        initCommonTab();
        initVp();
    }

    private void initCommonTab() {
        ArrayList<TabEntity> mTabEntities = new ArrayList<>();
        for (String tab : tabs) {
            mTabEntities.add(new TabEntity(tab, 0, 0));
        }
        commonTabLayout.setTabData(mTabEntities);
    }

    private void initVp() {
        vp.setOffscreenPageLimit(pageCount);
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                if (position == 0) {
                    if (soldUpFragment == null) {
                        soldUpFragment = new SoldFragment();
                        soldUpFragment.isDc = isDc;
                        soldUpFragment.type = 1;
                        soldUpFragment.saleType = productType;
                    }
                    currentFragment = soldUpFragment;
                }
                if (position == 1) {
                    if (soldDraftFragment == null) {
                        soldDraftFragment = new SoldFragment();
                        soldDraftFragment.isDc = isDc;
                        soldDraftFragment.type = 0;
                        soldDraftFragment.saleType = productType;
                    }
                    currentFragment = soldDraftFragment;
                }
                if (position == 2) {
                    if (soldDownFragment == null) {
                        soldDownFragment = new SoldFragment();
                        soldDownFragment.isDc = isDc;
                        soldDownFragment.type = 2;
                        soldDownFragment.saleType = productType;
                    }
                    currentFragment = soldDownFragment;
                }

                return currentFragment;
            }

            @Override
            public int getCount() {
                return pageCount;
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(this);
        tvSearch.setOnClickListener(this);
        commonTabLayout.setOnTabSelectListener(this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String status = intent.getStringExtra("status");

        setIntent(intent);
        if ("0".equals(status)) {
            vp.setCurrentItem(1);
            commonTabLayout.setCurrentTab(1);
        }

        refreshList();
    }

    public void refreshList() {
        if (soldUpFragment != null)
            soldUpFragment.refreshWithHeader();
        if (soldDownFragment != null)
            soldDownFragment.refreshWithHeader();

        if (soldDraftFragment != null)
            soldDraftFragment.refreshWithHeader();

    }

    /**
     * 设置对应tab上显示数量
     */
    public void setTabCount() {
        ArrayList<TabEntity> mTabEntities = new ArrayList<>();
        for (int i = 0; i < tabs.length; i++) {
            if (i == 0) {
                tabs[i] = "已上架(" + upCount + ")";
            }
            if (i == 1) {
                tabs[i] = "草稿(" + draftCount + ")";
            }
            if (i == 2) {
                tabs[i] = "已下架(" + downCount + ")";
            }
        }
        for (String tab : tabs) {
            mTabEntities.add(new TabEntity(tab, 0, 0));
        }
        commonTabLayout.setTabData(mTabEntities);
    }


    private void showSearchResult() {
//        commonTabLayout.setVisibility(View.VISIBLE);
//        ArrayList<TabEntity> mTabEntities = new ArrayList<>();
//        for (String tab : tabs) {
//            mTabEntities.add(new TabEntity(tab, 0, 0));
//        }
//        commonTabLayout.setTabData(mTabEntities);
        if (vp.getVisibility() == View.GONE) {
            vp.setVisibility(View.VISIBLE);
        }

        if (soldUpFragment != null) {
            soldUpFragment.refresh();
        }
        if (soldDraftFragment != null) {
            soldDraftFragment.refresh();
        }
        if (soldDownFragment != null) {
            soldDownFragment.refresh();
        }
    }

    @Override
    protected View getToolBar() {
        View actionbar = this.inflaterView(R.layout.toolbar_search, null);
        ((EditText) actionbar.findViewById(R.id.et_content)).setHint("请输入商品名称");
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-1, this.getResources().getDimensionPixelSize(com.epfresh.api.R.dimen.action_bar_height));
        actionbar.setLayoutParams(lp);
        return actionbar;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;

            case R.id.tv_search:
                if (TextUtils.isEmpty(etContent.getText().toString())) {
                    return;
                }
                setKeyword(etContent.getText().toString());
                showSearchResult();
                hideSoftKeyboard(etContent);
                break;
        }
    }

    @Override
    public void onTabSelect(int position) {
        vp.setCurrentItem(position);
    }

    @Override
    public void onTabReselect(int position) {

    }

    /**
     * 手动显示软键盘
     *
     * @param view
     */
    public void showSoftKeyboard(View view) {
        Context context = view.getContext();
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        if (imm != null) {
            imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
        }
    }

    /**
     * 手动隐藏软键盘
     *
     * @param view
     */
    public void hideSoftKeyboard(View view) {
        Context context = view.getContext();
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this, ProductActivity.class));
        finish();
    }
}
